package com.shiliao.app.activity.personal.presenter;

import android.text.TextUtils;

import com.shiliao.app.activity.personal.entity.PersonalEntity;
import com.shiliao.app.activity.personal.service.IPersonalService;
import com.shiliao.app.activity.personal.service.impl.PersonalImpl;
import com.shiliao.app.activity.personal.view.PersonalView;
import com.shiliao.app.update.UpdateEntity;
import com.shiliao.app.utils.YMServerUtil;
import com.yimei.framework.mvp.presenter.IBasePresenter;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2017/9/8.
 */

public class PersonalPresenter extends IBasePresenter<IPersonalService> {
    private PersonalView personalView;
    private IPersonalService personalService;

    public PersonalPresenter(PersonalView personalView) {
        this.personalView = personalView;
        personalService = new PersonalImpl();
        setService(personalService);
    }


    public void request() {
        super.loadData();
        Map param = new HashMap();
        param.put("user_id", personalView.getUserId());
        personalService.request(YMServerUtil.ACTION_GETINFO, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", "...." + result.toString());
                PersonalEntity entity = ParseHelper.getModel(result.toString(), "", PersonalEntity.class);
                String mNickName = ParseHelper.getString(result.toString(), "nickname");
                String mPhone = ParseHelper.getString(result.toString(), "cellphone_no");
                String mDeviceid = ParseHelper.getString(result.toString(), "device_id");
                String weixinNum=ParseHelper.getString(result.toString(),"weixin");
                LogUtil.e("yzq", mNickName + mPhone);
                if (!TextUtils.isEmpty(mNickName))
                    personalView.setNickName(mNickName, mPhone,weixinNum);
                else
                    personalView.setNickName(mDeviceid, mPhone,weixinNum);
            }

            @Override
            public void onFail(int statusCode, Object result) {
                LogUtil.e("yzq", "...." + result.toString());
            }
        });
    }

    public void checkoutUpdate(String version, String userId){
        Map param = new HashMap();
        param.put("device_type", "android");
        param.put("app_version", version);
        param.put("user_id", userId);
        LogUtil.e("yzq", "更新：" + YMServerUtil.ACTION_UPDATE + version);
        personalService.request(YMServerUtil.ACTION_UPDATE, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", "更新：" + result);
                UpdateEntity updateEntity = ParseHelper.getModel(result.toString(), "data", UpdateEntity.class);
                if (personalView != null) {
                    personalView.showDownloadDialog(updateEntity);
                }
            }

            @Override
            public void onFail(int statusCode, Object result) {
                LogUtil.e("yzq", "更新：" + result);
            }
        });
    }

}
